import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getMenuList } from '@/api/menu'
import { generateRoutes } from '@/utils/route'

export const usePermissionStore = defineStore('permission', () => {
  const routes = ref([])
  const menuRoutes = ref([])

  // 生成路由
  const generateRoutesFn = async () => {
    try {
      const { data } = await getMenuList()
      const accessedRoutes = generateRoutes(data)
      routes.value = accessedRoutes
      menuRoutes.value = accessedRoutes
      return accessedRoutes
    } catch (error) {
      console.error('获取路由失败:', error)
      return []
    }
  }

  return {
    routes,
    menuRoutes,
    generateRoutesFn
  }
}) 